//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include<stdio.h>
#include<stdlib.h>
#include <time.h>
#include"receive.h"
#include"Display.h"
#include "DevicePower.h"

/*global varible--------------------------------------------------------------------------------*/
int size;
void * pBuffer;
unsigned long cpu_mode;
bool bExit = 0;

//////////////////////////////////////////////////////////////////////////////////////////////////

unsigned GetCurTime()
{
    time_t t=time(NULL);
    tm * ptime=localtime(&t);
    return ((ptime->tm_hour *60) + ptime->tm_min) *60 + ptime->tm_sec;
}

unsigned GetMiliSeconds()
{
    SYSTEMTIME st;
    GetSystemTime(&st);// gets current time
    return st.wSecond*1000+st.wMilliseconds;
}

void Do_13M_Mode()
{
    for (int j = 0;j < 400;j++) {
        if(j % 80 == 0)Sleep(25);
        for (int i = 0;i < 1000;i++)
            for(int k = 0;k < 1000;k++);
    }
}

void Do_104M_mode()
{
    for (int j = 0;j < 300;j++) {
        if(j % 15 == 0)Sleep(30);
        for (int i = 0;i < 1000;i++)
            for(int k = 0;k < 1000;k++);
    }
}

void Do_208M_Mode()
{
    for (int j = 0;j < 185;j++) {
        if(j % 7 == 0)Sleep(40);
        for (int i = 0;i < 1000;i++)
            for(int k = 0;k < 1000;k++);
    }
}

void Do_312M_Mode()
{
    for (int j = 0;j < 90;j++) {
        if(j % 3 == 0)Sleep(50);
        for (int i = 0;i < 1000;i++)
            for(int k = 0;k < 1000;k++);
    }
}

void Do_406M_Mode()
{
    Sleep(1800);
}

ULONG __stdcall Simulate(void * arg)
{
    Sleep(10*1000);//wait for the system to boot.
    while (1) {
//        GetThreadTimes(GetCurrentThread(),&dummy,&dummy,&s_start,&u_start);

        switch (cpu_mode) {//simulate the cpu frequency change.
            printf("the mode is %d\n",cpu_mode);
            case 1:{
                Do_13M_Mode();
                break;
            }
            case 2:
            case 3:{
                Do_104M_mode();
                break;
            }
            case 4:{
                Do_208M_Mode();
                break;
            }
            case 5:{
                Do_312M_Mode();
                break;
            }
            default:{
                Do_406M_Mode();
                break;
            }
        }

//        GetThreadTimes(GetCurrentThread(),&dummy,&dummy,&s_end,&u_end);
//        INT64 time=FileTimeToQuadWork(&u_end) - FileTimeToQuadWork(&u_start) +
//                   FileTimeToQuadWork(&s_end) - FileTimeToQuadWork(&s_start);
//
//        int base=1800+1-(unsigned long)time/10000;
//        printf("the time is %d,base is %d\n",time,base);
//        if(base>0)Sleep(base);
    }
}

void Initialize_Simulate()
{
    HANDLE hThread=CreateThread(NULL,0,Simulate,NULL,0,NULL);
    if (hThread == NULL) {
        fprintf(stderr,"Error......Fail to CreateThread");
        exit(1);
    }
}

int main()
{
    Displayer displayer(434,14,200,10,2);
    sleep(2000);
    Initialize_Simulate();
    Receive_Initialize();
    pBuffer=new Device_Power[MaxDevice];
    size=MaxDevice;
    while (!bExit) {
        ReceiveEx(pBuffer);
        displayer.Display();
    }
    return 0;
}
